package com.franzliszt.maillist.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.franzliszt.maillist.R;
import com.franzliszt.maillist.util.ContactsInfo;

import java.util.List;

public class AllManAdapter extends RecyclerView.Adapter<AllManAdapter.ViewHolder> {
    private List<ContactsInfo> mContactsInfo;
    private OnClick onClick;

    public AllManAdapter(List<ContactsInfo> mCityInfo){
      this.mContactsInfo = mCityInfo;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from( parent.getContext() ).inflate( R.layout.allman_recyclerview_item,null,false );
        return new ViewHolder( view ,onClick);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
       ContactsInfo cityInfo = mContactsInfo.get( position );
        holder.ContactsName.setText( cityInfo.getmContactsName());
        holder.layout.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null){
                    onClick.OnClickListener( v,position );
                }
            }
        } );
    }

    @Override
    public int getItemCount() {
        return mContactsInfo.size();
    }
    public int getPositionForSection(char section) {
        for (int i = 0; i < getItemCount(); i++) {
            String group = mContactsInfo.get(i).getGroup();
            char firstChar = group.charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }
    private void AddContacts(ContactsInfo contactsInfo){
        mContactsInfo.add(contactsInfo);
        notifyDataSetChanged();

    }
    private void DeleteContacts(int position){
        mContactsInfo.remove(position);
        notifyDataSetChanged();
    }
    public void Freshen(){
        notifyDataSetChanged();
    }
    //回调接口
    public interface OnClick{
        void OnClickListener(View view,int Position);
    }
    //点击事件
    public void setOnclick(OnClick onclick) {
        this.onClick = onclick;
    }
    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView ContactsName;
        private LinearLayout layout;
        private OnClick Listener;

        public ViewHolder(@NonNull View itemView,OnClick Listener) {
            super( itemView );
            this.Listener = Listener;
            ContactsName = itemView.findViewById( R.id.ContactsName );
            layout = itemView.findViewById(R.id.layout);
        }

        @Override
        public void onClick(View view) {
            if (Listener != null){
                Listener.OnClickListener( view,getPosition() );
            }
        }
    }
}
